package com.jiesone.logger.distributecleaning.controler;
/*
 *@ClassName LogDataAPIContrer
 *@Description TODO
 *@Author tangsixiang@163.com
 *@Date 2022/1/17 14:18
 *@Version 1.0
 */


import com.alibaba.fastjson.JSON;
import com.jiesone.logger.distributecleaning.entity.RequestCause;
import com.jiesone.logger.distributecleaning.entity.RequsetLoggerSubmit;
import com.jiesone.logger.distributecleaning.entity.ResponseMes;
import com.jiesone.logger.distributecleaning.handler.DataProcessingHandler;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.PageRequest;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ExecutionException;


@Api(tags = "对外接口说明")
@RestController(value = "report")
public class LogDataAPIContrer {

 Logger logger = LoggerFactory.getLogger(LogDataAPIContrer.class);
 @Value("${logger-filebeat.kafkaTopicName}")
 private String  kafkaTopicName;
//    @Autowired
//    RestHighLevelClient highLevelClient;
//

    @Autowired
    JdbcTemplate jdbcTemplate;

//    @Autowired
    private KafkaTemplate<String ,Object> kafkaTemplate;
@Autowired
    DataProcessingHandler  processingHandler;
    /**
     * 根因上报接口
     * @param request
     * @return
     */
    @ApiOperation("根因分析数据上报接口")
    @RequestMapping(method ={RequestMethod.POST},value ="/reportRootCauseForm" )
     public ResponseMes reportRootCauseForm( RequestCause  request){
        logger.info(JSON.toJSONString(request));

        jdbcTemplate.update("INSERT INTO`logger_rootcase`(source_code,source_name,logger_mes,case_mes,event_time,creat_time)VALUE(?,?,?,?,?,now())",new Object[]{request.getSourceCode(),request.getSourceName(),request.getLoggerMes(),request.getCauseMes(),request.getEventTime()});
        return  new ResponseMes(100,"ok");

    }
    @ApiOperation("根因分析数据上报接口")
    @RequestMapping(method ={RequestMethod.POST},value ="/reportRootCauseRest" )
    public ResponseMes reportRootCauseRest(@RequestBody RequestCause  request){
        logger.info(JSON.toJSONString(request));

        jdbcTemplate.update("INSERT INTO`logger_rootcase`(source_code,source_name,logger_mes,case_mes,event_time,creat_time)VALUE(?,?,?,?,?,now())",new Object[]{request.getSourceCode(),request.getSourceName(),request.getLoggerMes(),request.getCauseMes(),request.getEventTime()});
        return  new ResponseMes(100,"ok");

    }


    /**
     * 读取数据放到 消息队列中 让业务继续处理
     * @param loggerSubmit
     * @return
     */

    @ApiOperation("日志详情上报接口")

    @RequestMapping(method = {RequestMethod.POST},value = "/loggersumbitForm")
    public  ResponseMes     loggerSubmitInerfaceForm( RequsetLoggerSubmit loggerSubmit  ){
       //   processingHandler.pullApartStringMessage(loggerSubmit.getSourceCode(),loggerSubmit.getLoggerMes());

        logger.info(loggerSubmit+"----------------");
    //    logger.info(kafkaTopicName.split(",")[0]+"---------------------");

        Map  map=new HashMap();
        map.put("message",loggerSubmit.getLoggerMes());
        map.put("@timestamp",System.currentTimeMillis());
        processingHandler.pullApartStringMessage(loggerSubmit.getSourceCode(),JSON.toJSONString(map));
      //  processingHandler.pullApartStringMessage(loggerSubmit.getSourceCode(),loggerSubmit.getLoggerMes());
       /* ListenableFuture<SendResult<String, Object>> resp = kafkaTemplate.send(kafkaTopicName.split(",")[0], loggerSubmit.getSourceCode(), loggerSubmit.getLoggerMes());
        try {
            logger.info("----------"+resp.get().toString());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }*/
        logger.info("--------相应--------"+JSON.toJSONString(loggerSubmit));
        return  new ResponseMes(100,"ok");
    }
    @ApiOperation("日志详情上报接口")
    @RequestMapping(method = {RequestMethod.POST},value = "/loggersumbitRest")
    public  ResponseMes     loggerSubmitInerfaceRest( RequsetLoggerSubmit loggerSubmit  ){


        Map  map=new HashMap();
            map.put("message",loggerSubmit.getLoggerMes());
        map.put("@timestamp",System.currentTimeMillis());
        processingHandler.pullApartStringMessage(loggerSubmit.getSourceCode(),JSON.toJSONString(map));


        //   processingHandler.pullApartStringMessage(loggerSubmit.getSourceCode(),loggerSubmit.getLoggerMes());
//        ListenableFuture<SendResult<String, Object>> resp = kafkaTemplate.send(kafkaTopicName.split(",")[0], loggerSubmit.getSourceCode(), loggerSubmit.getLoggerMes());
//        try {
//            logger.info("----------"+resp.get().toString());
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        } catch (ExecutionException e) {
//            e.printStackTrace();
//        }
//        logger.info("--------相应--------"+JSON.toJSONString(loggerSubmit));
        return  new ResponseMes(100,"ok");

    }


}
